package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workbench.domain.WbVisitLog;
import com.ruoyi.workbench.domain.WbWholeVisitLog;
import com.ruoyi.workbench.service.IWbVisitLogService;
import com.ruoyi.workbench.utils.Checker;
import com.ruoyi.workbench.utils.DateUtil;
import com.ruoyi.workbench.utils.Generator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 拜访日志Controller
 *
 * @author hanjiafeng
 * @date 2021-02-25
 */
@Controller
@RequestMapping("/workbench/channel_visit_log")
public class WbChannelVisitLogController extends BaseController {
    private final String prefix = "workbench/channel_visit_log";
    private final IWbVisitLogService wbVisitLogService;

    public WbChannelVisitLogController(IWbVisitLogService wbVisitLogService) {
        this.wbVisitLogService = wbVisitLogService;
    }

    @RequiresPermissions("workbench:channel_visit_log:view")
    @GetMapping()
    public String visitLog() {
        return prefix + "/channel_visit_log";
    }

    /**
     * 查询拜访日志列表
     */
    @RequiresPermissions("workbench:channel_visit_log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WbVisitLog wbVisitLog, HttpServletRequest httpServletRequest) {
        startPage();

        WbWholeVisitLog wbWholeVisitLog = Generator.generateWholeVisitLog(wbVisitLog, httpServletRequest);

        DateUtil dateUtil = Generator.initDateUtil(httpServletRequest);

        List<WbWholeVisitLog> list = wbVisitLogService.selectChannelWbWholeVisitLogList(wbWholeVisitLog, dateUtil.getPeriod(), dateUtil.getStartTimeStr(), dateUtil.getEndTimeStr());
        return getDataTable(list);
    }

    /**
     * 导出拜访日志列表
     */
    @RequiresPermissions("workbench:channel_visit_log:export")
    @Log(title = "信息上报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WbVisitLog wbVisitLog, HttpServletRequest httpServletRequest) {
        WbWholeVisitLog wbWholeVisitLog = Generator.generateWholeVisitLog(wbVisitLog, httpServletRequest);

        DateUtil dateUtil = Generator.initDateUtil(httpServletRequest);

        List<WbWholeVisitLog> list = wbVisitLogService.selectChannelWbWholeVisitLogList(wbWholeVisitLog, dateUtil.getPeriod(), dateUtil.getStartTimeStr(), dateUtil.getEndTimeStr());
        ExcelUtil<WbWholeVisitLog> util = new ExcelUtil<>(WbWholeVisitLog.class);
        return util.exportExcel(list, "信息上报", ShiroUtils.getLoginName());
    }

    /**
     * 新增拜访日志
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存拜访日志
     */
    @RequiresPermissions("workbench:channel_visit_log:add")
    @Log(title = "信息上报", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WbVisitLog wbVisitLog) {
        Long unitId7 = wbVisitLog.getUnitId7();
        if (Objects.isNull(unitId7)) {
            return AjaxResult.error("未选择小区/街道/自然庄");
        }

        //检测用户信息是否正确,正确就插入,否则直接返回异常信息
        AjaxResult ajaxResult = Checker.checkUserCommit(wbVisitLog);
        if (ajaxResult.get(AjaxResult.CODE_TAG).equals(AjaxResult.Type.SUCCESS.value())) {
            ajaxResult = toAjax(wbVisitLogService.insertWbVisitLog(wbVisitLog));
        }
        return ajaxResult;
    }

    /**
     * 修改拜访日志
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap modelMap) {
        WbVisitLog wbVisitLog = wbVisitLogService.selectWbWholeVisitLogById(id);
        modelMap.put("wbVisitLog", wbVisitLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存拜访日志
     */
    @RequiresPermissions("workbench:channel_visit_log:edit")
    @Log(title = "信息上报", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbVisitLog wbVisitLog) {
        //检测用户信息是否正确,正确就插入,否则直接返回异常信息
        AjaxResult ajaxResult = Checker.checkUserCommit(wbVisitLog);
        if (ajaxResult.get(AjaxResult.CODE_TAG).equals(AjaxResult.Type.SUCCESS.value())) {
            ajaxResult = toAjax(wbVisitLogService.updateWbVisitLog(wbVisitLog));
        }
        return ajaxResult;
    }

    /**
     * 删除拜访日志
     */
    @RequiresPermissions("workbench:channel_visit_log:remove")
    @Log(title = "信息上报", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wbVisitLogService.deleteWbVisitLogByIds(ids));
    }

    @GetMapping("/almost_expire")
    public String almostExpire() {
        return prefix + "/almost_expire";
    }

    @PostMapping("/getAlmostExpireList")
    @ResponseBody
    public TableDataInfo getAlmostExpireList(Integer period,Boolean isContainPast) {
        startPage();

        List<WbWholeVisitLog> list = wbVisitLogService.selectChannelExpiringWbWholeVisitLogList(new WbWholeVisitLog(), period,isContainPast);
        return getDataTable(list);
    }
}
